package com.digua.base.util

import android.view.Gravity
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.Toast
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.blankj.utilcode.util.ViewUtils

class Toasts {

    companion object {
        private var toast: Toast? = null

        private fun makeText(text: CharSequence, gravity: Int, duration: Int) {
            if (StrUtil.isBlank(text)) return
            val activity = ActivityUtils.getTopActivity()
            activity?.runOnUiThread { show(text, gravity, duration) } ?: show(text, gravity, duration)
        }

        private fun show(text: CharSequence, gravity: Int, duration: Int) {
            try {
                if (toast == null) {
                    toast = Toast.makeText(ActivityUtils.getTopActivity(), text, duration)
                } else {
                    toast?.duration = duration
                }
                toast?.setText(text)
                if (gravity != 0) {
                    toast?.setGravity(gravity, 0, 200)
                } else {
                    toast?.setGravity(Gravity.BOTTOM, 0, 200)
                }
                toast?.show()
            } catch (e: Exception) {
                LogUtil.e("异常:", e)
            }
        }

        /**
         * 显示一段较长时间的提示
         *
         * @param res
         */
        fun showLong(res: Int) = showLong(ActivityUtils.getTopActivity().getString(res))

        fun showLong(message: CharSequence) = ViewUtils.runOnUiThread { if (message.isNotBlank()) ToastUtils.showLong(message) }

        /**
         * 显示一段较短的提示
         *
         * @param res
         */
        fun showShort(res: Int) = showShort(ActivityUtils.getTopActivity().getString(res))

        fun showShort(message: CharSequence) = ViewUtils.runOnUiThread { if (message.isNotBlank()) ToastUtils.showShort(message) }

        /**
         * 在time时间内，不重复显示
         *
         * @param message 显示内容
         * @param time    间隔时间(毫秒)
         */
        fun showDeduplication(message: CharSequence, time: Long) {
            if (StrUtil.isBlank(message)) return
            val date = SPUtils.getLong(message.toString())
            if (System.currentTimeMillis() - date <= time) return
            showLong(message)
            SPUtils.put(message.toString(), System.currentTimeMillis())
        }

        /**
         * 在10s内，不重复显示
         *
         * @param message 显示内容
         */
        fun showDeduplication(message: CharSequence) = showDeduplication(message, 10000)

        /**
         * 在指定位置显示
         *
         * @param gravity
         * @param res
         */
        fun showInGravity(gravity: Int, res: Int) = showInGravity(gravity, ActivityUtils.getTopActivity().getString(res))

        fun showInGravity(gravity: Int, message: CharSequence) = makeText(message, gravity, Toast.LENGTH_SHORT)

        /**
         * 显示提示和图标
         *
         * @param drawable
         * @param res
         */
        fun showWithIcon(drawable: Int, res: Int) = showWithIcon(drawable, ActivityUtils.getTopActivity().getString(res))

        fun showWithIcon(drawable: Int, message: CharSequence) {
            try {
                val toast = getToast(drawable, message)
                toast.show()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

        /**
         * 在指定位置显示提示和图标
         *
         * @param drawable
         * @param res
         */
        fun showWithIconInGravity(drawable: Int, gravity: Int, res: Int) = showWithIconInGravity(drawable, gravity, ActivityUtils.getTopActivity().getString(res))

        fun showWithIconInGravity(drawable: Int, gravity: Int, message: CharSequence) {
            try {
                val toast = getToast(drawable, message)
                toast.setGravity(gravity, 0, 0)
                toast.show()
            } catch (e: Exception) {
                LogUtil.e("异常:", e)
            }
        }

        private fun getToast(drawable: Int, message: CharSequence): Toast {
            val toast = Toast.makeText(ActivityUtils.getTopActivity(), message, Toast.LENGTH_SHORT)
            try {
                val linearLayout = toast.view as LinearLayout?
                val iv = ImageView(ActivityUtils.getTopActivity())
                iv.setImageResource(drawable)
                linearLayout!!.addView(iv, 0)
            } catch (e: Exception) {
                LogUtil.e("异常:", e)
            }
            return toast
        }

    }
}